package com.snail.manager.user.service.Impl;

import com.alibaba.fastjson.JSON;

import com.snail.manager.user.dao.UserMapper;
import com.snail.manager.user.entity.Result;
import com.snail.manager.user.entity.User;
import com.snail.manager.user.entity.UserExample;
import com.snail.manager.user.service.ProduceService;
import com.snail.manager.user.service.RegisterService;
import com.snail.manager.user.utils.ResultUtils;
import com.snail.manager.user.utils.UUIDUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.shiro.crypto.hash.SimpleHash;
import org.apache.shiro.util.ByteSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.mail.MessagingException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class RegisterServiceImpl implements RegisterService {

    @Autowired
    private UserMapper userMapper;
    @Autowired
    private ProduceService produceService;
    public Result<User> check(String param, Integer type) {

        UserExample example = new UserExample();
        UserExample.Criteria criteria =example.createCriteria();

        //1.username      2.email
        if(type==1){
            criteria.andUsernameEqualTo(param);
        }if (type==2){
            criteria.andEmailEqualTo(param);
        }

        List<User> list =  userMapper.selectByExample(example);
        if (list==null||list.isEmpty()){
            return ResultUtils.success();
        }

        return ResultUtils.failure("用户名或邮箱重复",400);


    }

    public Result<User> register(User user) throws MessagingException {
        if(StringUtils.isEmpty(user.getUsername())||StringUtils.isEmpty(user.getPassword())){
            return ResultUtils.failure("用户名或密码不能为空！",400);
        }
        Result<User> result = check(user.getUsername(),1);
        if (result.getCode()==400){
            return ResultUtils.failure("用户名已存在！",400);
        }
        if(user.getEmail()!=null){
            result = check(user.getEmail(),2);
            if( result.getCode()==400){
                return ResultUtils.failure("邮箱已被注册",400);
            }
        }

        user.setState(false);
        user.setCode(UUIDUtils.getUUIDCode());
        /*user.setPassword(DigestUtils.md5DigestAsHex(user.getPassword().getBytes()));*/
        String hashAlgoritheName = "MD5";
        Object credentials = user.getPassword();
        Object salt = ByteSource.Util.bytes(user.getUsername());
        int hashIteration = 1;
        Object password = new SimpleHash(hashAlgoritheName,credentials,salt,hashIteration);
        user.setPassword(String.valueOf(password));
        Map<String,String> mailParam = new HashMap<String, String>();
        mailParam.put("email",user.getEmail());
        mailParam.put("code",user.getCode());
        String message = JSON.toJSONString(mailParam);

        try {
            userMapper.insertSelective(user);
        }catch (Exception e){
            e.printStackTrace();
            return ResultUtils.failure("数据库异常！",400);
        }

        produceService.sendMessage(message);//在此使用ACTivate实现异步消息发送
        // MailUtils.sendEmail(user.getEmail(),user.getCode());
        return ResultUtils.success();
}
}
